package com.zs.toolbox.web2j.system.core.controller.admin;


import com.zs.toolbox.web2j.common.exception.WtResponses;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.common.request.WtPageQuerySearchModel;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtAdminBaseController;
import com.zs.toolbox.web2j.common.web.WtAuthContext;
import com.zs.toolbox.web2j.system.core.entity.WtUserEntity;
import com.zs.toolbox.web2j.system.core.entity.WtUserWithRoleEntity;
import com.zs.toolbox.web2j.system.core.request.WtUserRoleRequest;
import com.zs.toolbox.web2j.system.core.service.WtUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户基本信息操作
 * </p>
 *
 * @author eric.zhang
 * @since 2018-09-21
 */
@RequestMapping("/api/v1/admin/user/")
@Api(description = "运营用户管理")
@Slf4j
@RestController
public class WtAdminUserController extends WtAdminBaseController<WtUserService, WtUserEntity,Long> {
    @Override
    public WtBaseResponse<WtPageBodyModel<WtUserEntity>> getPage(@RequestBody  WtPageQuerySearchModel wtPageQueryModel) {
        wtPageQueryModel.setSearchKey(getTableField(WtUserEntity.class,wtPageQueryModel.getSearchKey()));
        return super.getPage(wtPageQueryModel);
    }

    @ApiOperation("根据ID查询用户信息及其角色")
    @RequestMapping(value = "getDetail", method = {RequestMethod.POST, RequestMethod.GET})
    public WtBaseResponse<WtUserWithRoleEntity> getRoleWithPermissionById(Long id) {
        if (id == null) {
            id = WtAuthContext.getUserId();
        }

        WtUserWithRoleEntity wtUserWithRoleEntity = baseService.getUserWithRoleById(id);
        return new WtBaseResponse<>(wtUserWithRoleEntity);
    }

    @Override
    @ApiOperation("添加运营管理员")
    public WtBaseResponse<String> save(WtUserEntity wtUserEntity) {
        wtUserEntity.setUserType(2);
        return super.save(wtUserEntity);
    }


    @ApiOperation("修改用户角色")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "saveUserRole", method = {RequestMethod.POST, RequestMethod.GET})
    public WtBaseResponse<String> saveUserRole(WtUserRoleRequest request) {
        this.volidate(request);
        baseService.saveUserRole(request.getUserId(), request.getRoleIds(), WtAuthContext.getUserId());
        return WtResponses.SUCCESS;
    }

}
